﻿using System;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Web;


namespace RealState.Utils
{
    public static class StringMethods
    {
        public static string Capitalize(this string s)
        {
            if (s.Length < 2)
            {
                return s.ToUpperInvariant();
            }
            return (s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower());
        }

        public static string CapitalizeWords(this string s)
        {
            string[] strArray = s.Split(new char[] { ' ' });
            if (strArray.Length <= 1)
            {
                return s.Capitalize();
            }
            string str = string.Empty;
            foreach (string str2 in strArray)
            {
                str = str + str2.Trim().Capitalize() + " ";
            }
            return str.Trim();
        }

        public static string HtmlDecode(this string s)
        {
            return HttpContext.Current.Server.HtmlDecode(s);
        }

        public static string HtmlEncode(this string s)
        {
            return HttpContext.Current.Server.HtmlEncode(s);
        }

        public static bool IsDate(this string s)
        {
            DateTime time;
            return DateTime.TryParse(s, out time);
        }

        public static bool IsDecimal(this string s)
        {
            decimal num;
            return decimal.TryParse(s, out num);
        }

        public static bool IsInteger(this string s)
        {
            int num;
            return int.TryParse(s, out num);
        }

        public static string Left(this string s, int length)
        {
            if (s.Length < length)
            {
                return s;
            }
            return s.Substring(0, length);
        }

        public static string NullToEmptyString(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            return s;
        }

        public static string ReplaceCharsWith(this string s, char[] charsToReplace, string replacement)
        {
            string str = s;
            foreach (char ch in charsToReplace)
            {
                str = str.Replace(ch.ToString(), replacement);
            }
            return str;
        }

        public static string Slash(this string s)
        {
            return s.SlashLeft().SlashRight();
        }

        public static string SlashLeft(this string s)
        {
            if (s.Length == 0)
            {
                return "/";
            }
            if (s.Substring(0, 1) == "/")
            {
                return s;
            }
            return ("/" + s);
        }

        public static string SlashRight(this string s)
        {
            if (s.EndsWith("/"))
            {
                return s;
            }
            return (s + "/");
        }

        public static string StripHtml(this string s)
        {
            return Regex.Replace(s.HtmlDecode().Replace("<br/>", "").Replace("<br />", "").Replace("<BR/>", "").Replace("<BR />", ""), @"<(.|\n)*?>", string.Empty);
        }

        public static string SubStringKeepWords(this string s, int length)
        {
            int num = s.Length;
            if (num > length)
            {
                num = length;
            }
            string str = s.Substring(0, num);
            int num2 = str.LastIndexOf(" ");
            return str.Substring(0, num2);
        }

        public static bool ToBoolean(this string s)
        {
            return Convert.ToBoolean(s);
        }

        public static DateTime ToDateTime(this string s)
        {
            return Convert.ToDateTime(s);
        }

        public static double ToDouble(this string s)
        {
            return Convert.ToDouble(s);
        }

        public static int ToInt32(this string s)
        {
            return Convert.ToInt32(s);
        }

        public static float ToSingle(this string s)
        {
            return Convert.ToSingle(s);
        }

        public static string UrlEncode(this string s)
        {
            return HttpContext.Current.Server.UrlEncode(s);
        }
    }
}
